<?php

namespace api\modules\v1\controllers;
use api\common\controllers\CommonController;
use api\common\models\Common;
use Yii;
use \yii\caching\FileDependency;

class CategoryController extends CommonController
{
    public $modelClass = 'api\modules\v1\models\Category';

    public $serializer = 'yii\rest\Serializer';

    public function actions()
    {
        return [
            'index' => [
                'class' => 'yii\rest\IndexAction',
                'modelClass' => $this->modelClass,
                'prepareDataProvider' => [$this, 'prepareDataProvider']
            ]
        ];
    }


    public function prepareDataProvider()
    {
        $modelClass = new $this->modelClass;
        $data = $modelClass->getFormatCategory();
        $data['default'] = $modelClass::getTree($data['default'],0);
        $data['collect'] = $modelClass::getTree($data['collect'],0);

        if($data['default'] == ''){
            $data['default'] = array();
        }

        if($data['collect'] == ''){
            $data['collect'] = array();
        }
        return $data;
    }



    public function actionIndex_v2()
    {
        $modelClass = new $this->modelClass;
        $data = $modelClass->getFormatCategory_v2();
        $data['default'] = $modelClass::getTree($data['default'],0);
        $data['collect']['default'] = $modelClass::getTree($data['collect']['default'],0);

        if($data['collect']['default'] == ''){
            $data['collect']['default'] = array();
        }

        if($data['default'] == ''){
            $data['default'] = array();
        }

        return $data;
    }


    /*
     * 地区栏目输出id
     * @param $area
     * @return mixed
     */
    public function actionArea($area){
        $modelClass = new $this->modelClass;
        $Model = new $modelClass();
        return $Model->getArea();
    }

    /*
     * 地区列表
     * @return mixed
     */
    public function actionArealist(){
        $modelClass = new $this->modelClass;
        $Model = new $modelClass();
        return $Model->getAreaList();
    }

}
